﻿var ProductsController = function($scope, $http)
{
    $scope.loadData = function () {
        $http.get("/api/ProductsApi/").success(
            function (data) {
                $scope.Products = data;
            });
    };

    $scope.sendData = function () {
        var changed = jQuery.grep($scope.Products, function (i) { return i.State > 1; });
        $http.put("/api/ProductsApi/Sync/", changed).success(
            function () {
                $scope.loadData();
            })};

    $scope.dateFormat = 'dd MMM yyyy';

    $scope.sort = function (option) {
        $scope.reverse = (($scope.predicate == option && option != null) ? !$scope.reverse : false);
        $scope.predicate = option;
    };

    $scope.filterExp = null;

    $scope.setFilter = function (option) {
        $scope.filterExp = option;
        if (option == 'non-del')
        {
            $scope.filterFunc = function (product) { return $scope.canDelete(product.State); }
        }
        else
        {
            $scope.filterFunc = function (product) { return true; }
        }
    };

    $scope.selectedProduct = null;

    $scope.newProduct = function () {
        return Object.create(
        {
            Id: null,
            Name: null,
            Date: null,
            Description: null,
            State: 2
        });
    };

    $scope.productToEdit = $scope.newProduct();

    $scope.isEditOpen = false;

    $scope.edit = function (product) {
        $scope.isEditOpen = true;
        $scope.selectedProduct = product;
        angular.copy(product, $scope.productToEdit);
        $scope.productToEdit.DateOnly = new Date(product.Date).toISOString().split('T')[0];
    };

    $scope.cancelEdit = function () {
        $scope.isEditOpen = false;
    };

    $scope.completeEdit = function () {
        if ($scope.selectedProduct != null) {
            $scope.productToEdit.Date = new Date($scope.productToEdit.DateOnly);
            angular.copy($scope.productToEdit, $scope.selectedProduct);
            delete $scope.selectedProduct.DateOnly;
            $scope.selectedProduct.State = $scope.selectedProduct.State | 4;
        }
    };

    $scope.addNew = function () {
        $scope.productToEdit.Date = new Date($scope.productToEdit.DateOnly);
        var newProd = angular.copy($scope.productToEdit);
        delete newProd.DateOnly;
        newProd.State = 2;
        $scope.Products.push(newProd);
    };

    $scope.clearEdit = function () {
        $scope.selectedProduct = null;
        $scope.productToEdit = $scope.newProduct();
    };

    $scope.deleteItem = function (product) {
        product.State = (product.State & 0xef) | 8;
    };

    $scope.undeleteItem = function (product) {
        product.State = (product.State & 0xf7) | 16;
    };

    $scope.canDelete = function (state) {
        return (state & 1) == 0 && (state & 8) == 0 || (state & 16) != 0;
    };

    $scope.synch = function () {
        $scope.sendData();
    };

    $scope.loadData();
}
